package reflect;

import reflect.annotation.AutoRunClass;
import reflect.annotation.AutoRunMenthod;

import javax.management.remote.rmi._RMIConnection_Stub;
import java.io.File;
import java.lang.reflect.Method;

public class Test5 {
    public static void main(String[] args) throws Exception {
        File dir = new File(
                Test3.class.getResource(".").toURI()
        );

        String packName = Test5.class.getPackage().getName();
        System.out.println(packName);

        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File sub : subs) {
            String fileName = sub.getName();
            String className = fileName.substring(0, fileName.indexOf("."));
            Class cls = Class.forName(packName + "." + className);
//            boolean tf = cls.isAnnotationPresent(AutoRunClass.class);
//            if (tf){
//                Object obj = cls.newInstance();
//                Method[] methods = cls.getDeclaredMethods();
//                for (Method method: methods) {
//                    boolean jo = method.isAnnotationPresent(AutoRunMenthod.class);
//                    if (jo){
//                        method.invoke(obj);
//                    }
//                }
//            }
            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                System.out.println(className + "被标识了！");
                Object obj = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(AutoRunMenthod.class)) {
                        System.out.println(method.getName() + "标识了！");
                        method.invoke(obj);
                    }
                }
            }
        }
    }
}
